package com.example.bank.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.example.bank.domain.Result;
import com.example.bank.exception.SystemException;
import com.example.bank.pojo.User;
import com.example.bank.service.user.UserService;
import com.example.bank.util.JWTUtil;
import com.example.bank.util.ResultUtil;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author ：Leeziqiang
 * @description：user控制器
 * @date ：2021/12/3 20:55
 */
@RestController
@RequestMapping("/user")
public class UserController {

    private static final Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    // 用户(业务员)注册
    @PostMapping("/login")
    public Map<String, Object> login(String id, String username) {
        logger.info("----------用户{}执行了登录操作----------", id);
        if ("undefined".equals(id) || StrUtil.isEmpty(id) || StrUtil.isEmpty(username)) {
            throw new SystemException("param error");
        }
        logger.info("UserController---login---完成登录操作");
        return userService.login(id, username);
    }

    @PostMapping("/admin")
    public Map<String, Object> admin(String username, String password) {
        User user = userService.adminLogin(username, password);
        if (ObjectUtil.isNull(user)) {
            throw new SystemException("账号或密码错误");
        }
        String token = JWTUtil.sign(user.getUserId(), JWTUtil.SECRET);
        return ResultUtil.sign(token, 3);
    }

    // 经理查看自己的业务员
    @PostMapping("/list")
    @RequiresRoles("经理")
    public Result listSalesman() {
        String userId = JWTUtil.obtainId();
        List<User> users = userService.listByAgentId(userId);
        logger.info("UserController---list---完成查看业务员操作");
        return ResultUtil.success(users);
    }




}
